﻿using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveProductProgramHandbookCategoryServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveProductHandbookCategoryRequest, RetrieveProductHandbookCategoryResponse>
    {
        #region Properties
        public string Dao;
        public string Lang;
        #endregion
        public RetrieveProductProgramHandbookCategoryServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveProductHandbookCategoryRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Lang))
            {
                Lang = "en";
            }
            else
            {
                Lang = request.Lang;
            }
        }

        protected override bool OnExecute(RetrieveProductHandbookCategoryResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<ProductHandbookCategoryRecord> records = new List<ProductHandbookCategoryRecord>();
            List<ProductHandbookProduct> listProductItem = Providers.ProductHandbooks.RetrieveProductProgramHandbookCategory();
            Dictionary<int, ProductCategory> listCategory = Providers.ProductHandbooks.RetrieveListCategory().ToDictionary(x => x.CategoryId);
            Dictionary<int, ProductHandbookProductDetail> listProductDetail = Providers.ProductHandbooks.RetrieveListProductDetail().ToDictionary(x => x.ProductId);

            records = listProductItem.GroupBy(u => u.CategoryId)
                                                  .Select(grp => new ProductHandbookCategoryRecord
                                                  {
                                                      Category = Lang.Equals("en") ? listCategory[grp.Key].CategoryNameEn : listCategory[grp.Key].CategoryName,
                                                      Product = grp.Select(x => new ProductHandbookProductRecord
                                                      {
                                                          ProductId = x.ProductId,
                                                          ProductName = Lang.Equals("en") ? x.ProductNameEn : x.ProductName,
                                                          HasData = x.ProductStatus == 2,
                                                          HasInfo = listProductDetail.ContainsKey(x.ProductId)
                                                      }).ToList()
                                                  })
                                                  .ToList();
            response.Records = records;
            return isSuccess;
        }
    }
}
